package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.poji.Course;
import com.lagou.poji.Course_Lesson;
import com.lagou.poji.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.CourseLessonService;
import com.lagou.service.impl.CourseContentImpl;
import com.lagou.service.impl.CourseLessonServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
    
    //展示对应课程章节与课程信息
    public void findSectionAndLessonByCourseId(HttpServletRequest request , HttpServletResponse response){
        try {//1.获取参数
             String course_id = request.getParameter("course_id");
            //2.业务处理
            CourseContentService contentService = new CourseContentImpl();
            List<Course_Section> sectionList = contentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));

            String result = JSON.toJSONString(sectionList);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //课程信息的回显
    public void findCourseById(HttpServletRequest request , HttpServletResponse response){
        try {
            String id = request.getParameter("course_id");

            CourseContentService courseContentService=new CourseContentImpl();

            Course course = courseContentService.findCourseByCourseId(Integer.parseInt(id));

            SimplePropertyPreFilter filter=new SimplePropertyPreFilter(Course.class,"id","course_name");

            String s = JSON.toJSONString(course, filter);

            response.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void saveOrUpdateSection(HttpServletRequest request,HttpServletResponse response){

        try {
            Map<String,Object> map= (Map) request.getAttribute("map");

            Course_Section section=new Course_Section();

            BeanUtils.populate(section,map);

            CourseContentService cs=new CourseContentImpl();

            //判断是否携带ID
            if(section.getId()==0){
                String s = cs.saveSection(section);
                response.getWriter().print(s);
            }else {
                String s = cs.updateSection(section);
                response.getWriter().print(s);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    public void updateSectionStatus(HttpServletRequest request , HttpServletResponse response){
        try {
            int id = Integer.parseInt(request.getParameter("id"));
            int status = Integer.parseInt(request.getParameter("status"));
            CourseContentService contentService = new CourseContentImpl();
            String result = contentService.updateSectionStatus(id, status);
            response.getWriter().println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response){

        try {
            Map<String,Object> map= (Map) request.getAttribute("map");

            Course_Lesson lesson=new Course_Lesson();

            BeanUtils.populate(lesson,map);

            CourseLessonService cls=new CourseLessonServiceImpl();

            //判断是否携带ID
            //判断是否携带ID
            if(lesson.getId()==0){
                String s = cls.saveLesson(lesson);
                response.getWriter().print(s);
            }else {
                String s = cls.updateLesson(lesson);
                response.getWriter().print(s);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
